<template>
  <div>
    <!-- 退货列表 -->
    <el-card class="return_card">
      <el-form :model="searchForm" ref="searchRef" inline>
        <el-form-item label="退换货单号" prop="returnOrderCode">
          <el-input
            v-model="searchForm.returnOrderCode"
            placeholder="请输入退换货单号"
            class="form_input"
          />
        </el-form-item>
        <el-form-item label="退换货类型" prop="returnType">
          <el-select
            v-model="searchForm.returnType"
            placeholder="请选择退换货类型"
            class="form_input"
          >
            <el-option label="全部" :value="0" />
            <el-option
              v-for="item in returnType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="Search"
            @click="getList()"
            class="btn"
          >
            查询
          </el-button>
          <el-button icon="Refresh" @click="handleReset" class="btn">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="mt-1">
      <el-table :data="tableData" style="height: calc(100vh - 15rem)">
        <el-table-column
          label="退换货单号"
          prop="returnOrderCode"
          align="center"
        />
        <el-table-column label="退换货类型" align="center">
          <template #default="{ row }">
            <el-tag type="primary">
              {{
                returnType.find((item) => item.value == row.returnType).label ||
                ""
              }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="退换货时间" align="center">
          <template #default="{ row }">
            {{ row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="退换货数量" prop="quantity" align="center" />
        <el-table-column label="退换货总金额(元)" align="center">
          <template #default="{ row }">
            {{ row.returnAmount ? (row.returnAmount / 100).toFixed(2) : 0 }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remark"
          align="center"
          show-overflow-tooltip
        />
        <el-table-column label="操作" align="center">
          <template #default="{ row }">
            <el-button type="primary" link @click="detailBtn(row.id)">
              <el-icon> <InfoFilled /> </el-icon>详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <PagingQuery
        :page="page"
        :pageSize="pageSize"
        :total="total"
        @getList="getList"
        class="mt-1"
      />
    </el-card>

    <el-drawer v-model="detail_drawer" destroy-on-close title="详情" size="80%">
      <DetailPage :detailId="detailId" />
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import PagingQuery from "@/components/PagingQuery.vue";
import DetailPage from "./component/DetailPage.vue";
import { handleApiResponse } from "@/utils/request.js";
import { GetReturnOrderList } from "../../../../wailsjs/go/api/OrderApi";
import { returnType } from "./returnType";

onMounted(() => {
  getList();
});

const props = defineProps({
  detailId: {
    type: Number,
    default: null,
  },
});

const searchRef = ref(null);
const searchForm = ref({
  returnOrderCode: "",
  returnType: 0,
});

const handleReset = () => {
  searchRef.value.resetFields();
  getList();
};

const tableData = ref([]);
const total = ref(0);
const page = ref(1);
const pageSize = ref(10);
async function getList(e) {
  if (e) {
    page.value = e.page;
    pageSize.value = e.pageSize;
  }

  handleApiResponse(() =>
    GetReturnOrderList({
      ...searchForm.value,
      page: page.value,
      pageSize: pageSize.value,
      oldOrderId: props.detailId,
    })
  ).then((res) => {
    if (res.code == 0) {
      tableData.value = res.data.list || [];
      total.value = res.data.total || 0;
    }
  });
}

// 详情
const detail_drawer = ref(false);
const detailId = ref(null); // 0 新增!0 编辑
const detailBtn = (id) => {
  detailId.value = id;
  detail_drawer.value = true;
};
</script>

<style scoped lang="scss">
.return_card :deep(.el-form-item) {
  margin-bottom: 0 !important;
}
</style>